<template>
  <div class="form-container">
    <div class="container">
      <!--  主题内容标题   -->
      <div class="flex contain-header">
        <div class="contain-title">更换用户头像</div>
      </div>
      <!--  基本信息显示    -->
      <div class="info-contain">
        <div>
          <a-avatar :src="'http://zctuchuang.oss-cn-shenzhen.aliyuncs.com/'+avatar" :size="70"/>
          <div class="userName">{{ userName }}</div>
        </div>
        <a-form class="info-header" :form="form" @submit="submitInfo" :label-col="{ span: 9 }" :wrapper-col="{ span: 14,offset: 1 }">
          <a-form-item label="头像图片" extra="仅可上传一张">
            <a-upload
                list-type="picture-card"
                :file-list="picList"
                @preview="handlePreviewImg"
                @change="handleChangeImg"
                @customRequest="picRequest"
                :beforeUpload="beforeUploadImg"
                :remove="removeImg"
            >
              <div v-if="picList.length < 1">
                <a-icon type="plus" />
                <div class="ant-upload-text">
                  上传
                </div>
              </div>
            </a-upload>
            <a-modal :visible="previewVisible" :footer="null" @cancel="handleCancel">
              <img alt="example" style="width: 100%" :src="previewImage" />
            </a-modal>
          </a-form-item>
          <a-row class="row btn-margin info-header">
            <div>
              <template>
                <a-button type="primary" html-type="submit">提交</a-button>
              </template>
              <a-button class="btn-space" @click="handleReset">重置</a-button>
            </div>
          </a-row>
        </a-form>
      </div>
    </div>
  </div>
</template>

<script>
import {changePwd} from "@/api";

export default {
  name: "ChangeAvatar",
  data() {
    return{
      form: this.$form.createForm(this, { name: 'coordinated' }),
      avatar: window.localStorage.getItem('avatar'),
      userName: window.localStorage.getItem('userName'),
      picList: [],
      previewVisible: false,
    }
  },
  methods: {
    //图片预览
    async handlePreviewImg(file) {
      if (!file.url && !file.preview) {
        file.preview = await getBase64(file.originFileObj);
      }
      this.previewImage = file.url || file.preview;
      this.previewVisible = true;
    },
    //文件删除
    removeImg(file) {
      const index = this.picList.indexOf(file);
      const newFileList = this.picList.slice();
      newFileList.splice(index, 1);
      this.picList = newFileList;
    },
    //封面图片上传变化事件
    handleChangeImg(info) {
      if (info.file.status === 'done') {
        this.picList = info.file.originFileObj
      }
    },
    //图片上传之前事件
    beforeUploadImg(file, picList){
      this.picList = [...this.picList, file];
      return new Promise((resolve, reject) => {
        let isLt1M = file.size / 1024 / 1024 <= 20;
        if (!isLt1M) {
          this.$message.error('图片大小不能超过20MB!');
          this.picList = []
          return reject(false);
        }
      })
      return false
    },
    //图片上传
    picRequest(){
      console.log("我是图片上传",this.picList)
    },
    handleCancel() {
      this.previewVisible = false;
    },
    //修改用户密码
    submitInfo(e) {
      let data
      e.preventDefault();
      this.form.validateFields((err, values) => {
        if (!err) {
          data = this.form.getFieldsValue()
          data.userId = window.localStorage.getItem('userId')
          if(data) {
            changePwd(data)
                .then((res) => {
                  if (res.status == 200){
                    this.$message.success('修改密码成功，请重新登录！');
                    this.handleReset()
                    this.$router.push('/login')
                  }else{
                    this.$message.info(res.msg);
                    this.handleReset()
                  }
                })
                .catch((err) => {
                  this.$message.error(err);
                  this.handleReset()
                })
          }
        }
      });
    },
    handleReset() {
      this.form.resetFields();
    }
  }
}
</script>

<style lang="less" scoped>
.flex{
  display: flex;
  flex-direction: row;
}
.form-container {
  display: flex;
  justify-content: center;
}
.form-contain {
  width: 1600px;
  padding: 0px 30px 0px 30px;
  border-radius: 10px;
  border: 1px solid #5DD5C8;
  margin: 50px 0px;
}
.container{
  width: 1032px;
  height: 100%;
  background-color: #ffffff;
  border-radius: 4px;
  border: 1px solid #eeeeee;
  padding: 10px 13px;
  margin: 50px 0px;
  padding-bottom: 60px;
}
.contain-header{
  justify-content: space-between;
  align-items: center;
}
.contain-title{
  font-family: MicrosoftYaHei;
  font-size: 18px;
  font-weight: bold;
  font-stretch: normal;
  letter-spacing: 1px;
  color: #1bcbb4;
}
.contain-left{
  align-items: center;
  cursor: pointer;
}
.title{
  font-weight: 600;
  font-size: 16px;
  margin-right: 10px;
}
.info-contain {
  display: flex;
  flex-direction: column;
  align-items: center;
  font-size: 16px !important;
  color: #686767;
  margin-top: 30px;
}
.info-header {
  margin-top: 40px;
}
.userName {
  font-weight: 600;
  font-size: 18px;
  text-align: center;
  margin-top: 8px;
}
.text_center {
  text-align: center;
}
.info-content {
  width: 600px;
}
.row-style {
  margin-top: 18px;
}
.nav-icon{
  color: #999999;
  font-size: 20px;
  text-align: center;
}
.num-style {
  color: #1bcbb4;
  font-weight: bold;
  font-size: 24px;
  cursor: pointer;
}
.num-style:hover {
  color: #65dbcb;
}
.form-contain {
  width: 600px;
  padding: 0px 20px 20px 20px;
  border-radius: 10px;
  border: 1px solid #1bcbb4;
  margin: 50px 0px;
}
.form-title {
  font-size: 20px;
  font-weight: bold;
  text-align: center;
  padding: 5px;
  border-bottom: 1px solid #97f3e9;
}
.border-right {
  border-right: 1px solid #d6dcdc;
}
.record-space {
  margin-top: 10px;
}
.row{
  display: flex;
  flex-direction: row;
  justify-items: center;
  justify-content: center;
  margin-bottom: 15px;
}
.btn-space{
  margin-left: 80px;
}
.btn-margin{
  margin-top: 30px;
}
</style>